package com.serviceImpl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.bean.Permission;
import com.bean.RolePermission;
import com.mapper.PermissionMapper;
import com.mapper.RolePermissionMapper;
import com.service.PermissionService;

/**
 * @author  author : 张鹏
 * @date createTime：2018年3月17日 下午3:44:22 
 * @Description: 该类的功能描述
 */
@Service
public class PermissionServiceImpl implements PermissionService{
	@Resource
	private PermissionMapper permissionMapper;
	
	@Resource
	private RolePermissionMapper rolePermissionMapper;
	
	
	@Override
	public List<Permission> getAllPermission() {
		// TODO Auto-generated method stub
		return permissionMapper.selectAll();
	}
	
	
	@Override
	public int createNewPermission(Permission p) {
		return permissionMapper.insert(p);
	}
	
	@Override
	public int updatePermission(Permission p) {
		return permissionMapper.updateByPrimaryKey(p);
	}
	
	@Override
	public int deletePermissionByPrimaryKey(int permissionId) {
		return permissionMapper.deleteByPrimaryKey(permissionId);
	}


	@Override
	public List<RolePermission> getPermissionByRoleId(int roleId) {
		// TODO Auto-generated method stub
		return rolePermissionMapper.selectByRoleId(roleId);
	}
	
	@Override
	public int createRolePermission(HttpServletRequest request,int roleId) {
		String[] permissionIdList = request.getParameterValues("permission");
		RolePermission rolePermission;
		//删除角色原本的权限
		rolePermissionMapper.deleteByRoleId(roleId);
		
		for (String permissionId:permissionIdList) {
			rolePermission=new RolePermission();
			rolePermission.setRoleId(roleId);
			rolePermission.setPermissionId(Integer.parseInt(permissionId));
			rolePermissionMapper.insert(rolePermission);
		}
		return 1;
	}
	
	@Override
	public List<Permission> getPermissionClassification() {
		// TODO Auto-generated method stub
		return permissionMapper.selectClassificationAll();
	}
	
	@Override
	public Permission getPermissionByPrimaryKey(int permissionId) {
		// TODO Auto-generated method stub
		return permissionMapper.selectByPrimaryKey(permissionId);
	}

}
